//
//  WCNOrderTCell.m
//  WCN
//
//  Created by 杨鑫 on 15/7/8.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNOrderTCell.h"
//
#import "Macros.h"
#import "WCMeetingtimeManager.h"
#import "WCImageManager.h"
#import "NSString+Utilities.h"
#import "WCImageView +setImage.h"
#import "WCTSetTextTool.h"
@implementation WCNOrderTCell
{
    NSTimer *_timer;
    WCMeetingtimeManager *_meetTimeManager;
    
}
- (void)awakeFromNib {
    // Initialization code
    self.borderView.layer.borderColor = kBorderColor.CGColor;
    self.borderView.layer.borderWidth = kOnePX;
    self.borderView.layer.cornerRadius = 2;
    
    self.patientAvatar.layer.cornerRadius = 21;
    self.patientAvatar.clipsToBounds = YES;
}

- (IBAction)chatAction:(id)sender {
    NSInteger leftMeetTime =[_meetTimeManager getCurrentLeftTime];
    if (self.chatBlock) {
        self.chatBlock(leftMeetTime);
    }
}
- (void)configWithOrder:( Order*)order withCatorgery:(NSInteger)cate  withConversations:(NSArray *)conversations
{
    
    //创建时间倒计时
    if (_meetTimeManager) {
        _meetTimeManager =nil;
    }
    
    
    if (_timer) {
        [_timer invalidate];
        _timer =nil;
    }
    
    
    
    [self.patientAvatar setImageWithUrl:order.patientAvatarFileId];
    NSLog(@"%@",order.service_Type.serviceName);
    self.careType.text =order.service_Type.serviceName;
    self.price.text = [NSString stringWithFormat:@"%.2f元", [order.amountPaid floatValue]];
    self.status.text = order.nursingOrder_Status.name;
    if (order.completeTime) {
        WSJSetTxt(self.updateTime, [NSString timeToString:order.completeTime], @"");
        //        self.updateTime.text = [NSString timeToString:order.completeTime];
    } else {
        NSString *str = [NSString stringWithFormat:@"%@ %@", order.apptDate, order.timePeriod];
        WSJSetTxt(self.updateTime,str, @"");
        //        self.updateTime.text = [NSString stringWithFormat:@"%@ %@", order.apptDate, order.timePeriod];
    }
    self.address.text = order.patientAddress;
    self.patientName.text = order.patientName;
    self.patientPhone.text = order.patientPhone;
    
    //是否来自分诊平台
    
    
    if ([order.isPlus integerValue] == 1) {
        self.isUrgent.hidden = NO;
        self.price.hidden = YES;
    }else{
        self.isUrgent.hidden = YES;
        self.price.hidden = NO;
    }
    
    //在线
    if ([order.service_Type.serviceMode integerValue] !=2 || cate ==0) {
        self.chatIcon.hidden =YES;
    }else{
        self.chatIcon.hidden =NO;
        //处理聊天按钮的 图片
        NSString *imageName;
        if ([order.onlineConsultationStatus isEqualToString:@"START"]) {
            imageName =@"chat_enable";
            //            self.chatBtn.enabled =YES;
            NSString *chater = [NSString stringWithFormat:@"patient_%@",order.patientPhone];
            //            NSInteger unreadCount = [self isEverChatedWithChatter:chater withConverSations:conversations];
            //            if (unreadCount>0) {
            //                NSLog(@"------拉取时间%@",order.onlineConsultationTime);
            //                imageName =@"chat_new";
            //                if (!_timer) {
            //                    _timer = [NSTimer  scheduledTimerWithTimeInterval:0.7f target:self selector:@selector(newMessageAnimation) userInfo:nil repeats:YES];
            //                    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
            //                }
            //            }
            
        }else{
            imageName =@"chat_unenable";
            //            self.chatBtn.enabled =NO;
            
        }
        [self.chatIcon setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        __weak typeof(self) weakSelf =self;
        //创建时间管理器
        if ([order.onlineConsultationStatus isEqualToString:@"START"]||[order.onlineConsultationStatus isEqualToString:@"NOSTART"]) {
            if (order.onlineConsultationTime!=0) {
                _meetTimeManager =[[WCMeetingtimeManager alloc]initWithTime:[order.onlineConsultationTime integerValue]];
                _meetTimeManager.timeOverBlock= ^(){
                    if (weakSelf.refreshBlock) {
                        weakSelf.refreshBlock();
                    }
                    
                };
            }
        }
        
        
        
        
    }
    
}
- (void)newMessageAnimation
{
    self.chatIcon.layer.transform = CATransform3DRotate(self.chatIcon.layer.transform, M_PI / 2, 0, 1, 0);
}

//- (NSInteger)isEverChatedWithChatter:(NSString *)chatter withConverSations:(NSArray *)conversations
//{
//    NSInteger unreadCount = -1;;
//    for (EMConversation *cov in conversations) {
//        if ([cov.chatter isEqualToString:chatter]){
//            unreadCount = cov.unreadMessagesCount;
//            break;
//        }
//    }
//    return unreadCount;
//    
//}
@end
